package com.antcom.antread.common.redis.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 *
 * @Author 中国蚂蚁技术有限公司
 * @create 2020/12/15 20:40
 */
@Component
public class RedisUtil {

    @Inject
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 缓存值无超时时间 基本对象
     *
     * @param key
     * @param value
     * @param <T>
     */
    public <T> void setCacheObj(final String key, final T value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 存值设置超时时间 基本对象
     *
     * @param key
     * @param value
     * @param timeout
     * @param <T>
     */
    public <T> void setCacheObj(final String key, final T value, final Long timeout, final TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    /**
     * 设置有效时间
     *
     * @param key
     * @param timeout
     * @return true=设置成功；false=设置失败
     */
    public boolean expire(final String key, final long timeout) {
        return redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }

    /**
     * 设置有效时间
     *
     * @param key
     * @param timeout
     * @param timeUnit
     * @return true=设置成功；false=设置失败
     */
    public boolean expire(final String key, final long timeout, final TimeUnit timeUnit) {
        return redisTemplate.expire(key, timeout, timeUnit);
    }

    /**
     * 取值
     *
     * @param key
     * @param <T>
     * @return
     */
    public <T> T getValue(final String key) {
        return (T) redisTemplate.opsForValue().get(key);
    }

    /**
     * 删除单个
     *
     * @param key
     * @param <T>
     */
    public <T> void delete(final String key) {
        redisTemplate.delete(key);
    }

    /**
     * 删除集合
     *
     * @param collection
     */
    public <T> void delete(final Collection collection) {
        redisTemplate.delete(collection);
    }

    /**
     * 缓存LIST集合
     *
     * @param key
     * @param list
     * @param <T>
     * @return
     */
    public <T> long setCacheList(final String key, final List<T> list) {
        Long count = redisTemplate.opsForList().rightPush(key, list);
        return count == null ? 0L : count;
    }

    /**
     * 获取LIST缓存集合对象
     *
     * @param key
     * @param <T>
     * @return
     */
    public <T> List<T> getCacheList(final String key) {
        return (List<T>) redisTemplate.opsForList().range(key, 0, -1);
    }

    /**
     * 缓存SET集合
     *
     * @param key
     * @param set
     * @param <T>
     * @return
     */
    public <T> long setCacheSet(final String key, final Set<T> set) {
        Long count = redisTemplate.opsForSet().add(key, set);
        return count == null ? 0 : count;
    }

    /**
     * 获取缓存SET
     *
     * @param key
     * @param <T>
     * @return
     */
    public <T> Set<T> getCacheSet(final String key) {
        return (Set<T>) redisTemplate.opsForSet().members(key);
    }

    /**
     * 缓存MAP
     *
     * @param key
     * @param <T>
     */
    public <T> void setCacheMap(final String key, final Map<String, T> map) {
        redisTemplate.opsForHash().putAll(key, map);
    }

    /**
     * 往HASH中存值
     *
     * @param key
     * @param hKey
     * @param value
     * @param <T>
     */
    public <T> void setCacheMapValue(final String key, final String hKey, final T value) {
        redisTemplate.opsForHash().put(key, hKey, value);
    }

    /**
     * 获取HASH中的值
     *
     * @param key
     * @param hKey
     * @param <T>
     * @return
     */
    public <T> T getCacheMapValue(final String key, final String hKey) {
        return (T) redisTemplate.opsForHash().get(key, hKey);
    }

    /**
     * 获取多个HASH缓存中的值
     *
     * @param key
     * @param collection
     * @param <T>
     * @return
     */
    public <T> List<T> getCacheMapValue(final String key, final Collection<Object> collection) {
        return (List<T>) redisTemplate.opsForHash().multiGet(key, collection);
    }

    /**
     * 获取所有的key
     *
     * @param patten 前缀
     * @return
     */
    public Collection<String> keys(final String patten){
        return redisTemplate.keys(patten);
    }

    /**
     * 判断值是否存在
     *
     * @param key
     * @return
     */
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

}
